package com.pandora.common.task;

import com.alibaba.fastjson.JSONObject;
import com.pandora.common.util.Constants;
import com.pandora.common.util.DateUtil;
import com.pandora.common.util.JedisUtil;
import com.pandora.common.util.VerifyUtil;
import com.pandora.credit.dto.push.PushBean;
import com.pandora.credit.entity.Customer;
import com.pandora.credit.entity.Message;
import com.pandora.credit.entity.Track;
import com.pandora.credit.mongo.entity.MongoMessage;
import com.pandora.credit.mongo.service.MongoMessageService;
import com.pandora.credit.service.CustomerService;
import com.pandora.credit.service.MessageService;
import com.pandora.credit.service.PushService;
import com.pandora.credit.service.TrackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.Date;
import java.util.LinkedList;

import java.util.List;

/**
 * @author developer
 */
public class Tasks {
    private Logger logger = LoggerFactory.getLogger(Tasks.class);

    @Autowired
    private TrackService trackService;
    @Autowired
    private MongoMessageService mongoMessageService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private PushService pushService;

    public void writeTrackIntoMysql(){
        logger.info("write_track_into_mysql");
        String item = JedisUtil.executeLpop(Constants.QUEUE_REDIS_URL, Constants.TASK_WRITE_TRACK_KEY);
        logger.info("item==={}", item);
        while (!VerifyUtil.isEmpty(item)){
            JSONObject dicItem = JSONObject.parseObject(item);
            String code = dicItem.getString("code");
            String[] codes = code.split("/");
            if(codes.length == 2){
                code = codes[0];
            }
            logger.info("code==={}", code);
            Track t = new Track();
            t.setCode(code);
            t.setAccount(dicItem.getString("account"));
            t.setCreateTime(Timestamp.valueOf(dicItem.getString("dt")));
            t.setTerminalId(dicItem.getString("terminal_id"));
            t.setUpdateTime(Timestamp.valueOf(dicItem.getString("dt")));
            trackService.save(t);
            item = JedisUtil.executeLpop(Constants.QUEUE_REDIS_URL, Constants.TASK_WRITE_TRACK_KEY);
        }
    }

    public void pushMessageToJpush(){
        logger.info("push_message_to_jpush");
        Date currentTime = new Date();
        List<String> regIds = new LinkedList<>();
        regIds.add("18071adc03386396e88");
        List<MongoMessage> messageList = mongoMessageService.queryByIsSent(Constants.MESSAGE_SEND_TYPE_SEND);
        logger.info("jpush messageList size {}", messageList.size());
        for(MongoMessage message : messageList){
            long messageId = message.getId();
            String content = message.getContent();
            Date sendTime = message.getSendTime();
            if(sendTime == null || currentTime.compareTo(sendTime) >= 0){
                //推送
                logger.info("coming into push...");
                PushBean pushBean = new PushBean();
                pushBean.setMsgContent(content);
                pushBean.setRegistrationIds(regIds);
                pushBean.setBadge(0);
                if(!VerifyUtil.isEmpty(content)){
                    pushBean.setSound("default");
                }
                pushService.sendPush(pushBean);
                logger.info("send push end...");
                MongoMessage mongoMessage = new MongoMessage();
                mongoMessage.setId(messageId);
                mongoMessage.setContent(content);
                if(sendTime != null){
                    mongoMessage.setSendTime(sendTime);
                }
                mongoMessage.setCreatedTime(message.getCreatedTime());
                mongoMessage.setIsSent(Constants.MESSAGE_SEND_TYPE_SENT);
                mongoMessageService.save(mongoMessage);

                String statusArea = Constants.CUSTOMER_STATUS_BOUND+","+Constants.CUSTOMER_STATUS_REGISTERED;
                List<Customer> custs = customerService.getByStatusArea(statusArea);
                for(Customer c : custs){
                    Message m = new Message();
                    m.setCustomerId(c.getId());
                    m.setObjectId(messageId);
                    m.setReadStatus(Constants.MESSAGE_SEND_TYPE_SENT);
                    m.setCreateTime(new Timestamp(System.currentTimeMillis()));
                    m.setUpdateTime(DateUtil.getNowTimestamp());
                    messageService.save(m);
                }
            }
        }
    }
}
